package com.shaoshaohuo.app.framework;

import android.annotation.SuppressLint;
import android.util.Log;

import com.shaoshaohuo.app.constant.AppConfig;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @Deprecated 替换为com.shaoshaohuo.app.framework.log.LogUtil
 */
@Deprecated
public class Logger {

    /**
     * logcat日志开关 true 输出 false 不输出
     */
    private static final boolean isLogCatEnabled = true;

    /**
     * 是否将log输出到SD卡log目录文件中 输出 false 不输出
     */
    private static final boolean isFileEnabled = true;

    /**
     * log级别、只打印mPrintLogLevel级别以上的LOG
     */
    private static final int mPrintLogLevel = Log.VERBOSE;

    private static final long FILE_LENGTH = 500 * 1024;
    private static final int mLogCount = 20;
    @SuppressLint("SimpleDateFormat")
    private static final SimpleDateFormat mDataFormat = new SimpleDateFormat("MM/dd_HH:mm:ss");

    // private static File mLogFile = null;
    private static HashMap<String, File> mLogFileMap = null;

    public static void d(Object tag, Object content) {
        debug(String.valueOf(tag), String.valueOf(content), Log.DEBUG, false, true);
    }

    public static void i(Object tag, Object content) {
        debug(String.valueOf(tag), String.valueOf(content), Log.INFO, false, true);
    }

    public static void e(Object tag, Object content) {
        debug(String.valueOf(tag), String.valueOf(content), Log.ERROR, false, true);
    }

    public static void e(String tag, Throwable e) {
        debug(String.valueOf(tag), Log.getStackTraceString(e), Log.ERROR, false, true);
    }

    public static void e(String tag, String e) {
        debug(String.valueOf(tag), e, Log.ERROR, false, true);
    }

    public static void w(Object tag, Object content) {
        debug(String.valueOf(tag), String.valueOf(content), Log.WARN, false, true);
    }

    public static void v(Object tag, Object content) {
        debug(String.valueOf(tag), String.valueOf(content), Log.VERBOSE, false, true);
    }

    /**
     * @param tag
     * @param content
     * @param isToFile log输出到logcat的同时是否打印到SD卡文件上 void
     * @Description:
     * @author YSL
     */
    public static void d(Object tag, Object content, boolean isToFile) {
        debug(String.valueOf(tag), String.valueOf(content), Log.DEBUG, isToFile, true);
    }

    public static void i(Object tag, Object content, boolean isToFile) {
        debug(String.valueOf(tag), String.valueOf(content), Log.INFO, isToFile, true);
    }

    public static void e(Object tag, Object content, boolean isToFile) {
        debug(String.valueOf(tag), String.valueOf(content), Log.ERROR, isToFile, true);
    }

    public static void w(Object tag, Object content, boolean isToFile) {
        debug(String.valueOf(tag), String.valueOf(content), Log.WARN, isToFile, true);
    }

    public static void v(Object tag, Object content, boolean isToFile) {
        debug(String.valueOf(tag), String.valueOf(content), Log.VERBOSE, isToFile, true);
    }

    @SuppressWarnings("unused")
    private static void debug(String tag, String content, int level, boolean isToFile, boolean isToLagcat) {

        // isToFile = true;

        if (!AppConfig.DEBUG) {
            return;
        }

        if (!isLogCatEnabled && !isFileEnabled) {
            return;
        }

        if (mPrintLogLevel > level) {
            return;
        }

        switch (level) {
        case Log.VERBOSE:
            if (isToLagcat && isLogCatEnabled) {
                Log.v(String.valueOf(tag), String.valueOf(content));
            }
            if (isToFile && isFileEnabled) {
                debugFile(tag, content, "__VERBOSE");
            }
            break;
        case Log.DEBUG:
            if (isToLagcat && isLogCatEnabled) {
                Log.d(String.valueOf(tag), String.valueOf(content));
            }
            if (isToFile && isFileEnabled) {
                debugFile(tag, content, "__DEBUG");
            }
            break;
        case Log.INFO:
            if (isToLagcat && isLogCatEnabled) {
                Log.i(String.valueOf(tag), String.valueOf(content));
            }
            if (isToFile && isFileEnabled) {
                debugFile(tag, content, "__INFO");
            }
            break;
        case Log.WARN:
            if (isToLagcat && isLogCatEnabled) {
                Log.w(String.valueOf(tag), String.valueOf(content));
            }
            if (isToFile && isFileEnabled) {
                debugFile(tag, content, "__WARN");
            }
            break;
        case Log.ERROR:
            if (isToLagcat && isLogCatEnabled) {
                Log.e(String.valueOf(tag), String.valueOf(content));
            }
            if (isToFile && isFileEnabled) {
                debugFile(tag, content, "__ERROR");
            }

            break;
        }
    }

    private static void debugFile(String tag, String content, String levelName) {

        try {

            StringBuffer sb = new StringBuffer();
            sb.append(mDataFormat.format(new Date(System.currentTimeMillis())));
            // long time = System.currentTimeMillis();

            File mLogFile = null;

            if (mLogFileMap == null) {
                mLogFileMap = new HashMap<String, File>();
            }

            if (mLogFileMap.containsKey(tag)) {
                mLogFile = mLogFileMap.get(tag);
            }

            if (mLogFile == null || mLogFile.length() > FILE_LENGTH || !mLogFile.exists()) {
                mLogFile = getLogfile(tag);
                if (!mLogFile.exists()) {
                    mLogFile.createNewFile();
                }
                mLogFileMap.put(tag, mLogFile);
            }

            // Log.d("YSL", "time:" + (System.currentTimeMillis() - time));
            writeToFile(mLogFile, sb.toString() + levelName + "____" + tag + " :", String.valueOf(content));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static boolean writeToFile(File file, String tag, String message) {
        if (file == null) {
            return false;
        }
        try {
            FileWriter fw = new FileWriter(file, true);
            fw.append(tag);
            fw.append("\n");
            fw.append("        " + message);
            fw.append("\n\n");
            fw.flush();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private static File getLogfile(String tag) {

        File file = new File(DirManager.getAppLogDirectory() + tag + File.separator);

        if (!file.exists()) {
            file.mkdirs();
        }

        @SuppressWarnings("unused") File[] listFiles = file.listFiles();

        for(int i = 0; i < mLogCount; i++) {
            File f = new File(DirManager.getLogFile(tag, i));
            long length = f.length();
            if (f.exists() && length < FILE_LENGTH) {
                return f;
            } else {
                if (!f.exists()) {
                    // StringBuffer sb = new StringBuffer();
                    // sb.append(mDataFormat.format(new
                    // Date(System.currentTimeMillis())));
                    // writeToFile(f, sb.toString(), "");
                    return f;
                }
            }
        }

        for(int i = 0; i < mLogCount - 1; i++) {
            File preFile = new File(DirManager.getLogFile(tag, i));
            File nextFile = new File(DirManager.getLogFile(tag, i + 1));

            if (nextFile.lastModified() - preFile.lastModified() < 0) {
                nextFile.delete();
                return nextFile;
            }

            if (i == (mLogCount - 2)) {
                File f = new File(DirManager.getLogFile(tag, 0));
                f.delete();
                return f;
            }
        }

        return null;
    }

    public static void d1(String tag, Object content) {
        debug("Tag1", String.valueOf(tag + "__" + content), Log.DEBUG, false, true);
    }

    public static void e1(String tag, Throwable e) {
        debug("Tag1", Log.getStackTraceString(e), Log.ERROR, false, true);
    }

    public static void e1(String tag, String e) {
        debug("Tag1", e, Log.ERROR, false, true);
    }

    public class LogTag {

        public static final String HTTP = "HTTP";
        public static final String DB = "DB";
        public static final String UI = "APP_UI";
        public static final String JS = "JS";
    }

}
